package com.finger.wx.assistant.util;

import java.io.DataOutputStream;
import java.io.File;

public class ToolAdb {

	/**
	 * 执行Shell命令
	 * 
	 * @param commands
	 *            要执行的命令数组
	 */
	public static boolean execShell(String cmd) {
		DataOutputStream os = null;
		Process process = null;
		boolean ret = false;
		try {
			// 获取root权限，这里大量申请root权限会导致应用卡死，可以把Runtime和Process放在Application中初始化
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());

			os.write(cmd.getBytes());
			os.writeBytes("\n");
			os.flush();

			os.writeBytes("exit\n");
			os.flush();

			int exitValue = process.waitFor();
			if (exitValue == 0) {
				ret = true;
			}
		} catch (Exception e) {
			ret = false;
			e.printStackTrace();
		}
		return ret;
	}

	public static boolean execShell(String cmd, String filepath) {
		DataOutputStream os = null;
		Process process = null;
		boolean ret = false;
		try {
			// 获取root权限，这里大量申请root权限会导致应用卡死，可以把Runtime和Process放在Application中初始化
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());

			os.write(cmd.getBytes());
			os.writeBytes("\n");
			os.flush();

			os.writeBytes("exit\n");
			os.flush();

			int exitValue = process.waitFor();
			if (exitValue == 0) {
				ret = true;
			}
		} catch (Exception e) {
			ret = false;
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 执行Shell命令
	 * 
	 * @param commands
	 *            要执行的命令数组
	 */
	public static boolean execShell(String[] commands) {
		DataOutputStream os = null;
		Process process = null;
		boolean ret = false;
		try {
			// 获取root权限，这里大量申请root权限会导致应用卡死，可以把Runtime和Process放在Application中初始化
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			for (String command : commands) {
				if (command == null) {
					continue;
				}
				os.write(command.getBytes());
				os.writeBytes("\n");
				os.flush();
			}
			os.writeBytes("exit\n");
			os.flush();
			int exitValue = process.waitFor();
			if (exitValue == 0) {
				ret = true;
			}
		} catch (Exception e) {
			ret = false;
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 判断当前手机是否有ROOT权限
	 *
	 * @return
	 */
	public static boolean isRoot() {
		boolean bool = false;

		try {
			if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())) {
				bool = false;
			} else {
				bool = true;
			}
		} catch (Exception e) {

		}
		return bool;
	}

	public static boolean getRootAuth() {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes("exit\n");
			os.flush();
			int exitValue = process.waitFor();
			if (exitValue == 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
